package br.com.ngame;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import br.com.ngame.core.GameCore;
import br.com.ngame.core.GameCoreListener;
import br.com.ngame.device.BasicAudioDevice;
import br.com.ngame.device.BasicInputDevice;
import br.com.ngame.device.VideoDevice;
import br.com.ngame.device.WindowedVideoDevice;
import br.com.ngame.manager.WindowedImageManager;
import br.com.ngame.manager.WindowedSoundManager;

public abstract class DoubleSizeWindowedGame implements GameCoreListener
{
	private static final long serialVersionUID = 1L;
	private GameCore game;
	private VideoDevice videoDevice;
	private JFrame gameWindow = null;
	private int width = 0;
	private int height = 0;

	public DoubleSizeWindowedGame()
	{
		log("WINDOW CREATE >");
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice device = env.getDefaultScreenDevice();
		GraphicsConfiguration gc = device.getDefaultConfiguration();
		
        gameWindow = new JFrame("", gc);
		boolean fullScreen = setGameWindow(gameWindow);
		gameWindow.pack();
		gameWindow.setSize(width, height);
		Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		gameWindow.setLocation(screen.width/2-gameWindow.getWidth()/2, screen.height/2-gameWindow.getHeight()/2);
		
        videoDevice = new WindowedVideoDevice(800, 600);
		gameWindow.setVisible(true);
		game = new GameCore(this);
		game.setVideoDevice( videoDevice );
		game.setAudioDevice( new BasicAudioDevice() );
		game.setInputDevice( new BasicInputDevice(gameWindow) );
		game.setImageManager( new WindowedImageManager(videoDevice) );
		game.setSoundManager( new WindowedSoundManager(gameWindow) );
		hideCursor();
		log("WINDOW CREATE <");
	}
	
	protected boolean setGameWindow(JFrame window)
	{
		window.setUndecorated(true);
		window.setIgnoreRepaint(true);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		return false;
	}
	
	protected GameCore getGame()
    {
    	return game;
    }

	public void log(String msg)
    {
    	System.out.println(msg);
    }
    
    public void setup()
    {
    	// personalizado nas classes filhas
    }

    public void cleanup()
    {
    	// personalizado nas classes filhas    	
    }

	public void enterMenu()
	{
//		 personalizado nas classes filhas
	}

	public void enterPause()
	{
//		 personalizado nas classes filhas
	}

	public void exitMenu()
	{
//		 personalizado nas classes filhas
	}

	public void exitPause()
	{
//		 personalizado nas classes filhas
	}
	
	protected void hideCursor()
	{
		ImageIcon emptyIcon = new ImageIcon(new byte[0]);
		Cursor invisibleCursor = gameWindow.getToolkit().createCustomCursor(emptyIcon.getImage(), new Point(0,0), "Invisible");
		gameWindow.setCursor(invisibleCursor);
	}
}